package qnb.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import qnb._common.easyui.Tree;
import qnb._common.vo.MessageVOFactory;
import qnb.entity.SysRole;
import qnb.service.SysRoleService;
import weixinkeji.vip.jweb.power.ann.JWPIdentifiter;

import java.io.IOException;
import java.util.List;

/**
 * @author huangxin
 * @date 2019/5/21-17:09
 */
@RequestMapping(value = "/sysRole", produces = "application/json; charset=utf-8")
@Controller

public class SysRoleController {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private SysRoleService sysRoleService;

    @JWPIdentifiter({"admin:role"})
    @GetMapping("/forward/sysRole")
    public String forwardToPage() {
        return "sysRole/sysRole";
    }


    @JWPIdentifiter({"admin:role"})
    @RequestMapping("findAllRole")
    @ResponseBody
    public List<SysRole> findAllRole() {
        return sysRoleService.getAllRole();
    }

    @JWPIdentifiter({"admin:role"})
    @PostMapping("/update")
    @ResponseBody
    public String update(@RequestParam("sysRoleList") String sysRoleListStr) throws IOException {
        List<SysRole> sysRoleList = objectMapper.readValue(sysRoleListStr, new TypeReference<List<SysRole>>() {
        });
        return MessageVOFactory.getUpdateMessage(sysRoleService.update(sysRoleList));
    }

    @JWPIdentifiter({"admin:role"})
    @PostMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam("id") Integer id) throws IOException {
        return MessageVOFactory.getDellMessage(sysRoleService.delete(id));
    }

    /**
     * @param id  角色id
     * @param ids 权限集合
     * @return
     */
    @JWPIdentifiter({"admin:role"})
    @PostMapping("/updatePowers")
    @ResponseBody
    public Object updateRolePowersById(@RequestParam("id") Integer id, @RequestParam("ids") List<Integer> ids) {
        return MessageVOFactory.getUpdateMessage(sysRoleService.updateRolePowersById(id, ids));
    }

    @JWPIdentifiter("admin:teacherPower")
    @RequestMapping("getRoleByTeacherId")
    @ResponseBody
    public List<Tree> readRoleByTeacherId(@RequestParam("id") String id) {
        return sysRoleService.getRoleByTeacherId(id);
    }

    @JWPIdentifiter("admin:teacherPower")
    @RequestMapping("updateTeacherRole")
    @ResponseBody
    public Object updateTeacherRole(@RequestParam("id") String id, @RequestParam("ids") List<Integer> ids) {
        return MessageVOFactory.getUpdateMessage(sysRoleService.updateTeacherRole(id, ids));
    }

    @JWPIdentifiter("admin:studentPower")
    @RequestMapping("getRoleByStudentId")
    @ResponseBody
    public List<Tree> getRoleByStudentId(@RequestParam("id") String id) {
        return sysRoleService.getRoleByStudentId(id);
    }
    @JWPIdentifiter("admin:studentPower")
    @RequestMapping("updateStudentRole")
    @ResponseBody
    public Object updateStudentRole(@RequestParam("id") String id, @RequestParam("ids") List<Integer> ids) {
        return MessageVOFactory.getUpdateMessage(sysRoleService.updateStudentRole(id, ids));
    }

}
